/**
 * 
 */
package com.arx.script;

import java.util.StringTokenizer;

import com.arx.common.GlobalConsts;
import com.arx.interactive.InteractiveObject;
import com.arx.script.flyweight.ScriptObject;
import com.arx.script.flyweight.ScriptTimer;
import com.arx.script.flyweight.ScriptVariable;
import com.arx.script.flyweight.StackedEvent;

/**
 * @author Donald
 */
abstract class ScriptCommonVars implements ScriptBase {
	private InteractiveObject	_CURIO				= null;
	private int					ActiveTimers;
	private String[]			AS_EVENT			= {
													"ON NULL",
													"ON INIT",
													"ON INVENTORYIN",
													"ON INVENTORYOUT",
													"ON INVENTORYUSE",
													"ON SCENEUSE",
													"ON EQUIPIN",
													"ON EQUIPOUT",
													"ON MAIN",
													"ON RESET",
													"ON CHAT",
													"ON ACTION",
													"ON DEAD",
													"ON REACHEDTARGET",
													"ON FIGHT",
													"ON FLEE",
													"ON HIT",
													"ON DIE",
													"ON LOSTTARGET",
													"ON TREATIN",
													"ON TREATOUT",
													"ON MOVE",
													"ON DETECTPLAYER",
													"ON UNDETECTPLAYER",
													"ON COMBINE",
													"ON NPC_FOLLOW",
													"ON NPC_FIGHT",
													"ON NPC_STAY",
													"ON INVENTORY2_OPEN",
													"ON INVENTORY2_CLOSE",
													"ON CUSTOM",
													"ON ENTER_ZONE",
													"ON LEAVE_ZONE",
													"ON INITEND",
													"ON CLICKED",
													"ON INSIDEZONE",
													"ON CONTROLLEDZONE_INSIDE",
													"ON LEAVEZONE",
													"ON CONTROLLEDZONE_LEAVE",
													"ON ENTERZONE",
													"ON CONTROLLEDZONE_ENTER",
													"ON LOAD",
													"ON SPELLCAST",
													"ON RELOAD",
													"ON COLLIDE_DOOR",
													"ON OUCH",
													"ON HEAR",
													"ON SUMMONED",
													"ON SPELLEND",
													"ON SPELLDECISION",
													"ON STRIKE",
													"ON COLLISION_ERROR",
													"ON WAYPOINT",
													"ON PATHEND",
													"ON CRITICAL",
													"ON COLLIDE_NPC",
													"ON BACKSTAB",
													"ON AGGRESSION",
			"ON COLLISION_ERROR_DETAIL",
			"ON GAME_READY",
			"ON CINE_END",
			"ON KEY_PRESSED",
			"ON CONTROLS_ON",
			"ON CONTROLS_OFF",
			"ON PATHFINDER_FAILURE",
			"ON PATHFINDER_SUCCESS",
			"ON TRAP_DISARMED",
			"ON BOOK_OPEN",
			"ON BOOK_CLOSE",
			"ON IDENTIFY",
			"ON BREAK", "ON STEAL",
			"ON COLLIDE_FIELD",
			"ON CURSORMODE",
			"ON EXPLORATIONMODE",
			null									};
	private InteractiveObject	EVENT_SENDER;
	private long				Event_Total_Count	= 0;
	private StackedEvent[]		eventstack			= new StackedEvent[GlobalConsts.MAX_EVENT_STACK];
	private boolean				GLOB				= false;
	InteractiveObject			IO_DEBUG			= null;
	private InteractiveObject	LASTSPAWNED;
	private long				lChangeWeapon;
	private boolean				LINEEND;
	// *************************************************************************************
	// returns pos after this word. //
	// -1 failure //
	// flags =0 standard //
	// =1 no INTERPRETATION (except for ~ ) //
	// *************************************************************************************
	/** the maximum number of script timers. */
	private int					MAX_TIMER_SCRIPT	= 0;

	/** the number of global script variables. */
	private int					NB_GLOBALS			= 0;
	private boolean				NEED_DEBUG;
	protected final boolean isNEED_DEBUG() {
		return NEED_DEBUG;
	}
	protected final void setNEED_DEBUG(boolean nEED_DEBUG) {
		NEED_DEBUG = nEED_DEBUG;
	}
	private boolean				pauseScript;
	protected boolean isPauseScript() {
		return pauseScript;
	}
	protected final void setPauseScript(boolean pauseScript) {
		this.pauseScript = pauseScript;
	}
	private InteractiveObject	pIOChangeWeapon;
	private ScriptTimer[]		scr_timer;
	private String[]			SSEPARAMS			= new String[GlobalConsts.MAX_SSEPARAMS];
	private long				STACK_FLOW			= 8;
	/** global script variables. */
	private ScriptVariable[]	svar				= new ScriptVariable[1];
	/**
	 * Gets the _CURIO
	 * @return {@link InteractiveObject}
	 */
	public final InteractiveObject get_CURIO() {
		return _CURIO;
	}
	/**
	 * Gets the activeTimers
	 * @return {@link int}
	 */
	public final int getActiveTimers() {
		return ActiveTimers;
	}
	protected final String getAsEvent(final int index) {
		String val = null;
		if (AS_EVENT != null
				&& index >= 0
				&& index < AS_EVENT.length) {
			val = AS_EVENT[index];
		}
		return val;
	}
	protected final InteractiveObject getEVENT_SENDER() {
		return EVENT_SENDER;
	}
	protected final StackedEvent getEventstack(final int index) {
		StackedEvent val = null;
		if (eventstack != null
				&& index >= 0
				&& index < eventstack.length) {
			val = eventstack[index];
		}
		return val;
	}
	/**
	 * Gets a global {@link ScriptVariable} by its index.
	 * @param index the index
	 * @return {@link ScriptVariable}
	 */
	protected final ScriptVariable getGlobalScriptVariable(final int index) {
		ScriptVariable val = null;
		if (svar != null
				&& index >= 0
				&& index < svar.length) {
			val = svar[index];
		}
		return val;
	}
	/**
	 * Gets the lASTSPAWNED
	 * @return {@link InteractiveObject}
	 */
	public final InteractiveObject getLASTSPAWNED() {
		return LASTSPAWNED;
	}
	/**
	 * Gets the mAX_TIMER_SCRIPT
	 * @return {@link int}
	 */
	public final int getMAX_TIMER_SCRIPT() {
		return MAX_TIMER_SCRIPT;
	}
	/**
	 * Gets the number of global script variables.
	 * @return int
	 */
	protected final int getNB_GLOBALS() {
		return NB_GLOBALS;
	}
	protected final ScriptTimer getScriptTimer(final int index) {
		ScriptTimer val = null;
		if (scr_timer != null
				&& index >= 0
				&& index < scr_timer.length) {
			val = scr_timer[index];
		}
		return val;
	}
	/**
	 * Gets a value from the SSEPARAMS.
	 * @param index
	 * @return
	 */
	protected final String getSSEPARAMS(final int index) {
		String val = null;
		if (SSEPARAMS != null
				&& index >= 0
				&& index < SSEPARAMS.length) {
			val = SSEPARAMS[index];
		}
		return val;
	}
	protected final long getSTACK_FLOW() {
		return STACK_FLOW;
	}
	protected abstract String GetVarValueInterpretedAsText(String temp1,
			ScriptObject esss, InteractiveObject io);
	protected final boolean isGLOB() {
		return GLOB;
	}
	protected final boolean isLINEEND() {
		return LINEEND;
	}
	/**
	 * Sets the _CURIO
	 * @param _CURIO the _CURIO to set
	 */
	public final void set_CURIO(final InteractiveObject _CURIO) {
		this._CURIO = _CURIO;
	}
	/**
	 * Sets the activeTimers
	 * @param activeTimers the activeTimers to set
	 */
	public final void setActiveTimers(final int activeTimers) {
		ActiveTimers = activeTimers;
	}
	protected final void setEVENT_SENDER(final InteractiveObject eVENT_SENDER) {
		EVENT_SENDER = eVENT_SENDER;
	}
	protected final void setEventstack(final int index, final StackedEvent val)
			throws Exception {
		if (eventstack != null
				&& index >= 0
				&& index < eventstack.length) {
			eventstack[index] = val;
		} else {
			throw new Exception(
					"ScriptCommonVars.setEventstack() - Invalid index referenced for event stack");
		}
	}
	protected final void setGLOB(final boolean gLOB) {
		GLOB = gLOB;
	}
	/**
	 * Sets a global script variable.
	 * @param index the array index being set
	 * @param val the {@link ScriptVariable}
	 */
	protected final void setGlobalScriptVariable(final int index,
			final ScriptVariable val) {
		if (svar != null
				&& index >= 0
				&& index < svar.length) {
			svar[index] = val;
		} else {
			// extend the length of the array
			ScriptVariable[] temp = new ScriptVariable[index + 1];
			System.arraycopy(svar, 0, temp, 0, svar.length);
			temp[index] = val;
			svar = temp;
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public final void ARX_SCRIPT_Free_All_Global_Variables() {
		if (svar != null) {
			for (int i = 0; i < svar.length; i++) {
				svar[i].clear();
			}
			NB_GLOBALS = 0;
		}
	}
	/** Initializes the script timers. */
	public final void initScriptTimers() {
		if (this.scr_timer == null) {
			scr_timer = new ScriptTimer[MAX_TIMER_SCRIPT];
		} else {
			for (int i = 0; i < scr_timer.length; i++) {
				if (scr_timer[i] != null) {
					scr_timer[i].reset();
				}
			}
			ScriptTimer[] temp = new ScriptTimer[MAX_TIMER_SCRIPT];
			System.arraycopy(scr_timer, 0, temp, 0, MAX_TIMER_SCRIPT);
			scr_timer = temp;
		}
	}
	/**
	 * Sets the lASTSPAWNED
	 * @param lASTSPAWNED the lASTSPAWNED to set
	 */
	public final void setLASTSPAWNED(final InteractiveObject lASTSPAWNED) {
		LASTSPAWNED = lASTSPAWNED;
	}
	protected final void setLINEEND(final boolean lINEEND) {
		LINEEND = lINEEND;
	}
	private final String underscore = 
			"_______________________________________________________________________________";
	/**
	 * Sets the mAX_TIMER_SCRIPT
	 * @param mAX_TIMER_SCRIPT the mAX_TIMER_SCRIPT to set
	 */
	public final void setMAX_TIMER_SCRIPT(final int mAX_TIMER_SCRIPT) {
		MAX_TIMER_SCRIPT = mAX_TIMER_SCRIPT;
	}
	protected final void MakeSSEPARAMS(final String params) {
		for (byte i = 0; i < GlobalConsts.MAX_SSEPARAMS; i++) {
			SSEPARAMS[i] = "";
		}
		if (params != null && params.length() > 0) {
			StringTokenizer token = new StringTokenizer(params, " ");
			int pos = 0;
			while (token.hasMoreTokens()) {
				if (pos < GlobalConsts.MAX_SSEPARAMS) {
					SSEPARAMS[pos] = token.nextToken();
				}
				pos++;
			}
		}
	}
	/**
	 * Sets the number of global script variables.
	 * @param val the number of global script variables to set
	 */
	protected final void setNB_GLOBALS(final int val) {
		NB_GLOBALS = val;
	}
	protected final void setSTACK_FLOW(final long sTACK_FLOW) {
		STACK_FLOW = sTACK_FLOW;
	}
	protected final long getEvent_Total_Count() {
		return Event_Total_Count;
	}
	protected final void setEvent_Total_Count(long event_Total_Count) {
		Event_Total_Count = event_Total_Count;
	}
}
